#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTableWidget>
#include <QPushButton>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSplitter>
#include <QFileDialog>
#include <QMessageBox>
#include <QGroupBox>
#include <QLabel>
#include <QTime>        // For timestamps in log
#include <QApplication> // For standard icons
#include <QStyle>       // For standard icons
#include <QFileInfo>    // For file operations
#include <QVariant>

#include "gui_bridge.h"                 // 包含gui_bridge.h以使用FileItem结构体
#include "transfer_progress_dialog.hpp" // Ensure this is included

// Forward declaration for C client functions/structs if needed
// extern "C" {
// void request_file_list();
// int upload_file_request(const char* filepath);
// int download_file_request(const char* filename_on_server, const char* save_path);
// void client_logout();
// }

// 前向声明
class ClientWrapper;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(const QString &username, QWidget *parent = nullptr);
    explicit MainWindow(const QString &username, ClientWrapper *clientWrapper, QWidget *parent = nullptr);
    ~MainWindow();

signals:
    void loggedOut(); // Signal to indicate user has logged out

public slots:
    // Slots to be called from C client logic (via a bridge)
    void updateFileList(const QList<FileItem> &files); // Or pass raw C structs and convert
    void appendLogMessage(const QString &message, bool isError = false);
    void updateTransferProgress(const QString &fileName, int progressPercentage, const QString &status); // For transfer_progress_dialog
    void onTransferCompletedInternal(const QString &filename, bool success, const QString &message);
    void handleUploadSuccess(const QString &fileName);
    void handleUploadFailure(const QString &fileName, const QString &errorMsg);
    void handleDownloadSuccess(const QString &fileName);
    void handleDownloadFailure(const QString &fileName, const QString &errorMsg);

private slots:
    void onUploadButtonClicked();
    void onDownloadButtonClicked();
    void onRefreshFileListClicked();
    void onLogoutButtonClicked();
    void onFileTableItemDoubleClicked(QTableWidgetItem *item);
    void onDeleteButtonClicked();
    void onDeleteFileResult(bool success, const QString &filename, const QString &message);

private:
    void setupUi();
    void setupConnections();
    void requestInitialFileList(); // Method to trigger initial file list load

    QString m_currentUser;
    QString m_currentRemotePath; // To keep track of the current path on the server

    // File Operations Area
    QGroupBox *m_fileOpsGroupBox;
    QTableWidget *m_fileTableWidget;
    QPushButton *m_uploadButton;
    QPushButton *m_downloadButton;
    QPushButton *m_deleteButton;
    QPushButton *m_refreshFileListButton;
    QPushButton *m_logoutButton;

    // Log Viewing Area
    QGroupBox *m_logViewGroupBox;
    QTextEdit *m_logTextEdit; // Simple text edit for logs
    // QTableWidget *m_logTableWidget; // Alternative for structured logs

    // Potentially use your existing transfer_progress_dialog
    TransferProgressDialog *m_transferDialog;

    void populateFileList(const QList<FileItem> &files);

    // ClientWrapper指针
    ClientWrapper *m_clientWrapper;
};

#endif // MAINWINDOW_H